//
//  UserAccountViewModel.swift
//  Sina
//
//  Created by yuanyuan on 15/12/20.
//  Copyright © 2015年 yuanyuan. All rights reserved.
//

import UIKit

import AFNetworking


class UserAccountViewModel: NSObject {
    
    //定义用户模型对象的属性
    var userAccount: UserAccount?
    
    
    override init() {
        
        //对象的构造 分 两步
        //先构造本来  再构造父类
        //        self.name = name
        userAccount = UserAccount.loadAccount()
        super.init()
    }
    
    //计算性属性 用户是否登陆
    var userLogin: Bool {
        return userAccount?.access_token != nil
    }
    
    //用户头像的url
    var headURL: NSURL? {
        let url = NSURL(string: userAccount?.avatar_large ?? "")
        return url
    }
    
    //用户名
    var userName: String? {
        return userAccount?.name
    }
    
    var token: String? {
        return userAccount?.access_token
    }

    
//加载用户token请求标识
    func loadAccessToken(code:String,finished:(isSuccess:Bool)->()) {
        let urlString = "https://api.weibo.com/oauth2/access_token"
        let parameters = ["client_id":client_id,"client_secret":client_secret,"grant_type":"authorization_code","code":code,"redirect_uri":redirect_uri]
////        let AFN = AFHTTPSessionManager()
//        AFN.responseSerializer.acceptableContentTypes?.insert("text/plain")
//        AFN.POST(urlString, parameters: parameters, success: { (_, result) -> Void in
//            
//            //            print(result)
//            //            获取token成功
//            //            获取用户信息
//            if let dict = result as? [String:AnyObject] {
////                let token = dict["access_token"] as! String
////                let uid = dict["uid"] as! String
////                self.loadUserInfo(token,uid:uid)
////                字典转模型
//                let userAccount = UserAccount(dict:dict)
//                self.loadUserInfo(userAccount, finished: finished)
//                
//            }
//            
//            
//            }) { (_, error) -> Void in
//                
//                print(error)
//                finished(isSuccess: false)
//        }

        NetworkTools.sharedTools.requestJsonDict(.POST ,urlString:urlString, parameters: parameters) { (dict, error) -> () in
            print(dict)
            if error != nil {
                //请求失败
                //执行失败的回调
                finished(isSuccess: false)
                return
            }
            //字典转模型
            let userAccount = UserAccount(dict: dict!)
            self.loadUserInfo(userAccount, finished: finished)
        }

    
    }
    
//    获取用户信息
    private func loadUserInfo(account:UserAccount,finished:(isSuccess:Bool)->()) {
        
        let urlString = "https://api.weibo.com/2/users/show.json"
        
        if let token = account.access_token, UserId = account.uid {
        let parameters = ["access_token": token, "uid": UserId]
        
//        let AFN = AFHTTPSessionManager()
//        AFN.GET(urlString, parameters: parameters, success: { (_, result) -> Void in
//            
////            print(result)
//            if let dict = result as? [String:AnyObject]{
//                
//                //            获取用户信息  头像 和 名字
//                let avatar_large = dict["avatar_large"] as! String
//                let name = dict["name"] as! String
////              给account对象的属性赋值
//                account.name = name
//                account.avatar_large = avatar_large
////                获取用户信息的完整自定义对象
////                存储自定义对象
//                account.saveAccount()
////                执行成功的回调
//                finished(isSuccess: true)
//                
//                
//                
////                print(avatar_large,name)
//                
//            }
//            
//            }) { (_, error) -> Void in
//                
//                finished(isSuccess: false)
////                print(error)
//                
//        }
//        
    
            NetworkTools.sharedTools.requestJsonDict(.GET, urlString: urlString, parameters: parameters, finished: { (dict, error) -> () in
                if error != nil {
                    //执行失败的回调
                    finished(isSuccess: false)
                    return
                }
                
                //获取用户信息
                let avatar_large = dict!["avatar_large"] as! String
                let name = dict!["name"] as! String
                //给account 对象的属性做赋值操作
                account.name = name
                account.avatar_large = avatar_large
                //获取用户信息的完整自定义对象
                //存储自定义对象
                account.saveAccount()
                //执行成功的回调
                finished(isSuccess: true)
            })

    }

    }
    

}
